package chapter2.src.com.kkb.array;

import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


class TestHashMap {
	
	HashMap<String,Object> map = new HashMap<>();
	
	{
		map.put("A","1");
		map.put("B","2");
		map.put("C","3");

	}

	public static void test(){
		System.out.println("重地".hashCode());
		System.out.println("通话".hashCode());
	}


	static final int hash(Object key) {
		int h;
		int hashcode = h = key.hashCode();
		System.out.println(Integer.toBinaryString(hashcode));

		int offset = h >>> 16;
		System.out.println(Integer.toBinaryString(offset));
		return   hashcode ^ offset ;  // 异或
	}

	public  static  HashMap<Integer, String> getIntHashMap(){


		HashMap<Integer, String> map = new HashMap<>();

		for (int i = 0; i < 16; i++) {

			map.put(i, "abc");
		}

		return map;
	}


	public static void main(String[] args)     {

		test();
		int hash = hash(1);
		int hash2 = hash(1);
		System.out.println(hash);
		System.out.println(hash2);
		//getIntHashMap();

		int key1 = hash("key1");
		int key11 = hash("key1");
		System.out.println(key1);
		System.out.println(key11);

	}





	@Test
	void testFor() {

		 for(String key : map.keySet()){
		     System.out.println(key);
		 }
		 System.out.println("====");
		 for(Object value : map.values()){
		     System.out.println(value);
		 }
	}
	
	@Test
	void testForGet() {

		 Set<String> keySet = map.keySet();
		 for(String str : keySet){
		     System.out.println(str+"-"+map.get(str));
		 }
	}
	
	
	@Test
	void testForEntry() {

		 Set<Map.Entry<String,Object>> entrySet = map.entrySet();
		 for(Map.Entry<String,Object> entry : entrySet){
		     System.out.println(entry.getKey()+"-"+entry.getValue());
		 }
	}
	
	@Test
	void testForIterator(){
		 Iterator<Map.Entry<String,Object>> iterator = map.entrySet().iterator();
		 while(iterator.hasNext()){
		    Map.Entry<String,Object> mapEntry = iterator.next();
		    System.out.println(mapEntry.getKey()+"-"+mapEntry.getValue());
	    }
	}
	
	

}
